//
//  XWInterestViewController.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 CXG. All rights reserved.
//

#import "XWInterestViewController.h"
#import "XWInterestCell.h"
#import <AFNetworking.h>
#import "XWInterestModel.h"
#import "XWCommentViewController.h"
#import "XWJokeViewController.h"
#import "HeadHelp.h"

@interface XWInterestViewController ()

// 帖子数据
@property(nonatomic,strong)NSMutableArray *interestArray;

@end

@implementation XWInterestViewController
static NSString * reusInterestCell = @"interestCell";

// 懒加载
-(NSMutableArray *)interestArray
{
    if (_interestArray == nil) {
        _interestArray = [[NSMutableArray alloc]init];
    }
    return _interestArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupTableView];
    
    // 数据解析
    [self parse];
    
}

-(void)setupTableView{
    self.tableView.backgroundColor =XWCommonColor;
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"XWInterestCell" bundle:nil] forCellReuseIdentifier:reusInterestCell];
    
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.tableView.tableHeaderView.backgroundColor = XWCommonColor;
    self.automaticallyAdjustsScrollViewInsets = NO;

}
// 请求数据
-(void)parse{
    // 参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"data";
    params[@"type"] = @"29";
   
    

    
    // 发送请求
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *array = responseObject[@"list"];
        
        for (NSDictionary *d in array) {
            
            XWInterestModel *model = [[XWInterestModel alloc]init];
//            NSLog(@"%@",d);

            
            [model setValuesForKeysWithDictionary:d];
            
            [self.interestArray addObject:model];
            NSLog(@"%ld",self.interestArray.count);


        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
        
            [self.tableView reloadData];
            
            
        });
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
}






#pragma mark - Table view data source


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%ld",self.interestArray.count);

    return self.interestArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XWInterestCell *cell = [tableView dequeueReusableCellWithIdentifier:reusInterestCell forIndexPath:indexPath];
    
    // 分割线样式
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // 赋值
    XWInterestModel *model = self.interestArray[indexPath.row];
    cell.model = model;
    
    
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XWInterestModel *model = self.interestArray[indexPath.row];
    XWInterestCell *cell = [[XWInterestCell alloc]init];
    cell.model = model;


    return 100 + [XWInterestCell calsLabelHeightWithTextComment:model.text];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XWCommentViewController *commentVC = [[XWCommentViewController alloc]init];
    
    [self.navigationController pushViewController:commentVC animated:YES];
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
